<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions(chartData) {
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: "15%",
          right: "3%",
          left: "10%",
          bottom: "18%"
        },

        xAxis: [
          {
            type: "category",
            data: chartData.map(v => {
              return v.alarmName;
            }),
            axisLine: {
              lineStyle: {
                color: "#fffff"
              }
            },
            axisLabel: {
              margin: 10,
              color: "#fffff",
              rotate: "30",
              textStyle: {
                fontSize: 12
              }
            }
          }
        ],
        yAxis: [
          {
            name: "单位：次数",
            axisLabel: {
              formatter: "{value}",
              color: "#e2e9ff"
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: "rgba(255,255,255,1)"
              }
            },
            splitLine: {
              lineStyle: {
                color: "rgba(255,255,255,0.12)"
              }
            }
          }
        ],
        series: [
          {
            type: "bar",
            data: chartData.map(v => {
              return v.alarmCount;
            }),
            barWidth: "14px",
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "#FCE149"
                  },
                  {
                    offset: 1,
                    color: "#EB893A"
                  }
                ]),
                // new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                //   {
                //     offset: 0,
                //     color: "#C0EB59"
                //   },
                //   {
                //     offset: 1,
                //     color: "#37C611"
                //   }
                // ])
                barBorderRadius: [30, 30, 30, 30]
              }
            },
            label: {
              normal: {
                show: true,
                lineHeight: 25,
                width: 70,
                height: 25,
                backgroundColor: "rgba(235, 139, 58,0.1)",
                borderRadius: 200,
                position: ["-11", "-40"],
                distance: 1,
                formatter: ["    {d|●}", " {a|{c}}     \n", "    {b|}"].join(
                  " "
                ),
                rich: {
                  d: {
                    color: "#FCE149"
                  },
                  a: {
                    color: "#fff",
                    align: "center"
                  },
                  b: {
                    width: 1,
                    height: 14,
                    borderWidth: 1,
                    borderColor: "#234e6c",
                    align: "left",
                    verticalAlign: "top"
                  }
                }
              }
            }
          }
        ]
      });
    }
  }
};
</script>
